from flask_appbuilder import ModelView
from flask_appbuilder.models.sqla.interface import SQLAInterface
from app.models import BackEnd
from app import appbuilder


class BackEndView(ModelView):
    datamodel = SQLAInterface(BackEnd)

    list_columns = ['host', 'port', 'active', 'desc']

    # show_template = 'appbuilder/general/model/show_cascade.html'

    def _add(self):
        """
            Add function logic, override to implement different logic
            returns add widget or None
        """
        widget = super(BackEndView, self)._add()
        if widget:
            form = widget['add'].template_args.get('form')
            form.active.flags.required = False
            form.active.validators.clear()
        return widget

    def _edit(self, pk):
        """
            Edit function logic, override to implement different logic
            returns Edit widget and related list or None
        """
        widgets = super(BackEndView, self)._edit(pk)
        if widgets:
            form = widgets['edit'].template_args.get('form')
            form.active.flags.required = False
            form.active.validators.clear()
        return widgets


appbuilder.add_view(BackEndView, "BackEnd", icon="fa-folder-open-o", category="Settings")

